﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseManager;
using Physio10Context;

namespace Physio_Clinic
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        public bool isnonDrug = true;
        public Qf8soUser currentUser = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["currentDrug"] != null && !Page.IsPostBack)
            {
                Drug drug = (Drug)Session["currentDrug"];

                DrugCodeTxt.Text = drug.DrugCode; 
                DetailTxt.Text = drug.DrugDetail;
                DrugNameTxt.Text = drug.DrugName ;
                PriceTxt.Text = drug.DrugPrice.ToString() ;
                AmountTxt.Text = drug.Amount.ToString();
            }

            if (Request.QueryString["nonDrug"] != null)
            {
                isnonDrug = false;
                Label1.Text = "ชื่อการรักษา";
                Label4.Text = "รายละเอียดการรักษา";

                TableRow2.Visible = false;
                TableRow7.Visible = false;

            }
            if (Session["currentUser"] != null)
            {
                currentUser = (Qf8soUser)Session["currentUser"];
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Drug drug = null;

            if (Session["currentDrug"] != null)
            {
                drug = (Drug)Session["currentDrug"];
            }
            else
            {
                drug = new Drug();
            }

            drug.DrugCode = DrugCodeTxt.Text;
            drug.DrugDetail = DetailTxt.Text;
            drug.DrugName = DrugNameTxt.Text;
            drug.DrugPrice = (int?)Convert.ToInt64(PriceTxt.Text);
            

            if (Session["currentDrug"] == null)
            {
                PersistentManager.Instance.Add(drug, currentUser.Username);
            }

            if (Session["nonDrug"] != null)
                drug.IsDrug = false;
            else
            {
                drug.IsDrug = true;
                drug.Amount = (int?)Convert.ToInt64(AmountTxt.Text);
            }
            PersistentManager.Instance.Save();
            Session["currentDrug"] = null;

            if (Session["nonDrug"] != null)
                Response.Redirect("DrugStock.aspx?nonDrug=1");
            else
                Response.Redirect("DrugStock.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            if (Session["nonDrug"] != null)
                Response.Redirect("DrugStock.aspx?nonDrug=1");
            else
                Response.Redirect("DrugStock.aspx");
        }
    }
}